﻿namespace DCHealthCheck.MvcWeb.Classes {

    #region Usings
    using DCHealthCheck.MvcWeb.Classes.ViewModels;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    #endregion

    /// <summary>
    /// Uses the IOC container to retrieve Model types by overriding CreateModel
    /// </summary>
    public class CustomViewModelBinder : DefaultModelBinder {

        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) {
            if (propertyDescriptor.PropertyType == typeof(DateTime)) {

                var model = bindingContext.Model;
                var modelPropertyInfo = model.GetType().GetProperty(propertyDescriptor.Name);
                var propertyValue = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);

                if (propertyValue != null) {
                    var dateTime = DateTime.MinValue;

                    if (DateTime.TryParseExact(propertyValue.AttemptedValue, "yyyy-MM-dd-HH-mm-ss-FFFFFFF", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) {
                        modelPropertyInfo.SetValue(model, dateTime, null);
                    }
                    else if (DateTime.TryParseExact(propertyValue.AttemptedValue, "yyyy-MM-dd-HH-mm-ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) {
                        modelPropertyInfo.SetValue(model, dateTime, null);
                    }
                    else if (DateTime.TryParseExact(propertyValue.AttemptedValue, "yyyy-MM-dd-HH-mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) {
                        modelPropertyInfo.SetValue(model, dateTime, null);
                    }
                    else if (DateTime.TryParseExact(propertyValue.AttemptedValue, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) {
                        modelPropertyInfo.SetValue(model, dateTime, null);
                    }
                    else if (DateTime.TryParse(propertyValue.AttemptedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) {
                        modelPropertyInfo.SetValue(model, dateTime, null);
                    }
                    else {
                        long ticks = 0;
                        if (long.TryParse(propertyValue.AttemptedValue, out ticks)) {
                            dateTime = DateTime.FromBinary(ticks);
                        }
                        else {
                            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
                        }
                    }
                }
            }
            else {
                base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
            }
        }

        ///// <summary>
        ///// Example of how to use DI with a view model
        ///// </summary>
        ///// <param name="controllerContext"></param>
        ///// <param name="bindingContext"></param>
        ///// <param name="modelType"></param>
        ///// <returns></returns>
        //protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) {
        //    if (modelType.BaseType == typeof(BaseViewModel) || modelType == typeof(BaseViewModel)) {
        //        return DependencyResolver.Current.GetService(modelType);
        //    }
        //    else {
        //        return base.CreateModel(controllerContext, bindingContext, modelType);
        //    }
        //}

    }
}